package academic.ui.client.presenter;

import java.util.ArrayList;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.AppServiceAsync;
import academic.ui.client.jquery.Facebook;
import academic.ui.client.jquery.JQuery;
import academic.ui.client.place.Tokens;
import academic.ui.client.widgets.DiscussionWidget;
import ecommerce.soco.shared.GWT_Discussion;
import ecommerce.soco.shared.GWT_Message;
import ecommerce.soco.shared.GWT_Product;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;

public class ProductPresenter extends Presenter<ProductPresenter.MyView,ProductPresenter.MyProxy>
{

	private final AppServiceAsync service;
	private final PlaceManager placeManager;
	
	@ContentSlot
	public static final Type<RevealContentHandler<?>> SLOT = new Type<RevealContentHandler<?>>();
	
	
	@Inject 
	public ProductPresenter(EventBus eventBus, MyView view, MyProxy proxy,AppServiceAsync service,PlaceManager placeManager) {
		super(eventBus, view, proxy);
		this.service = service;
		this.placeManager=placeManager;
	}
	
	public final String TOKEN_TYPE = "id";
	
	public String productID; 
	public String receipt;


	@ProxyStandard	
	public interface MyProxy extends Proxy<ProductPresenter>{

	}
	
	public interface MyView extends View {

		void setup(ArrayList<GWT_Product> product);

		HasClickHandlers buyButton();

		void hide();

		void fadeIn();

		void fadeOut();

		void setExpired(Boolean expired);

		ArrayList<GenericClickable> getClickHandlers();

		boolean isExpired();

		void setup(GWT_Product product);

		HasClickHandlers joinDiscussionLink();

		void setupDiscussionContent(GWT_Discussion isLoggedIn, Boolean isLoggedIn2);

		DiscussionWidget getDiscussionWidget();
	}
	
	@Override
	public void onReveal() {
		super.onReveal();
		Log.debug("ProductPresenter onReveal");
		DeferredCommand.addCommand(new Command() {
		
			@Override
			public void execute() {
				Facebook.init();
			}
	
		});
	
	}
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("ProductPresenter onBind");
		registerHandler(getView().buyButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				placeManager.revealPlace(new PlaceRequest(Tokens.checkout).with("id", productID));
			}
			
		}));
		
		registerHandler(getView().joinDiscussionLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				
			}
			
		}));
		
		registerHandler(getView().getDiscussionWidget().showLoginView().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				placeManager.revealPlace(new PlaceRequest(Tokens.login).with("selector", "loginLink"));
			}
			
		}));
		
		registerHandler(getView().getDiscussionWidget().submitMessageButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if(((GenericContainerTag)event.getSource()).getStyleName().contains("inactive"))
				{
					event.preventDefault();
					event.stopPropagation();
				}
				else
				{
					service.updateDiscussion(productID,getView().getDiscussionWidget().textAreaMessage().getText(),getView().getDiscussionWidget().emailUpdateValue().getValue(),new AsyncCallback<GWT_Message>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onSuccess(GWT_Message message) {
							getView().getDiscussionWidget().addMessage(message);
							getView().getDiscussionWidget().resetText();
							JQuery.slideMsgUp();
						}
						
					});
				}
					
			}
			
		}));
		
	}
	
	@Override
	public void onUnbind() {
		super.onUnbind();
		Log.debug("ProductPresenter onUnbind");
	}
    
	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, AppPresenter.slot, this);
		Log.debug("ProductPresenter revealInParent");
	}
	
	@Override
	public void onReset() {
		super.onReset();
		//ProductResetEvent.fire(getEventBus(), productID);
		//getView().fadeOut();
		getView().fadeIn();
		Log.debug("ProductPresenter onReset");
		if(getView().isExpired())
		{
			service.getFullProduct(Integer.parseInt(productID), new AsyncCallback<GWT_Product>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(GWT_Product product) {
					getView().setup(product);
				}
				
			});
		}
		else
			service.getFullWithActiveProducts(Integer.parseInt(productID), new AsyncCallback<ArrayList<GWT_Product>>() {
	
				@Override
				public void onFailure(Throwable arg0) {
					// TODO Auto-generated method stub
					
				}
	
				@Override
				public void onSuccess(ArrayList<GWT_Product> products) {
					getView().setup(products);
					service.isLoggedIn(new AsyncCallback<Boolean>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(final Boolean isLoggedIn) {
							service.getDiscussion(productID,new AsyncCallback<GWT_Discussion>() {

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub
									
								}

								@Override
								public void onSuccess(GWT_Discussion discussion) {
									getView().setupDiscussionContent(discussion,isLoggedIn);
								}
								
							});
						}
						
					});
					for(GenericClickable clickHandler : getView().getClickHandlers())
					{
						registerHandler(clickHandler.addClickHandler(new ClickHandler() {
	
							@Override
							public void onClick(ClickEvent event) {
								placeManager.revealPlace(new PlaceRequest(Tokens.productFull).with("id", event.getRelativeElement().getId()));
							}
							
						}));
					}
				}
				
			});
	}
	
	@Override
	public void onHide() {
		super.onHide();
		//getView().fadeOut();
		//getView().hide();
		Log.debug("ProductPresenter onHide");
	}

	

	
	

}
